// pages/confirm/index.js
const App = getApp()
Page({
  data:{
    photo_box_xk:[],
    photo_box_xc:[],
    index:99999999,
    index_xc:99999999,
    xk_list:[],
    xc_list:[],
    address:[],
    price:0,
    xk_price:0,
    xc_price:0,
  },
  onLoad:function(options){
    // 页面初始化 options为页面跳转所带来的参数s
    this.getPhotoBox()
    this.getSelectAddress()
  },
  onShow:function(){
    this.getPleseaeOrders()
    this.getSelectAddress()
  },
  getPhotoBox:function(){
    wx.showToast({
      title: '加载中...',
      icon: 'loading',
      duration: 20000,
      mask:true
    })
    var that=this
    App.HttpService.getPhotoBox().then(data=>{
      if(data.meat.code==0){
          wx.hideToast()
        that.setData({
          photo_box_xk:data.xk,
          photo_box_xc:data.xc,
          xk_list:data.xk_list,
          xc_list:data.xc_list
        })
      }
    })
  },
  //相框选择器
    bindPickerChange_xk: function(e) {
    var key=e.detail.value
    this.setData({
      index: e.detail.value,
      xk_price:(this.data.xk_list[key].price)
    })
    var price_sum=parseFloat(this.data.xk_price)+parseFloat(this.data.xc_price)
    this.setData({
      price:price_sum.toFixed(2)
    })
  },
  //相册选择器
    bindPickerChange_xc: function(e) {
      var key=e.detail.value
    this.setData({
      index_xc: e.detail.value,
      xc_price:(this.data.xc_list[key].price)
    })
    var price_sum=parseFloat(this.data.xk_price)+parseFloat(this.data.xc_price)
    this.setData({
      price:price_sum.toFixed(2)
    })
  },
  select_addRess:function(){
    wx.navigateTo({
      url: '../user/address/index',
    })
  },
  getSelectAddress:function(){
    var that=this
    wx.showToast({
      title: '加载中...',
      icon: 'loading',
      duration: 20000,
      mask:true
    })
    App.HttpService.getSelectAddRessInfo({
      user_id:wx.getStorageSync('user_id')
    }).then(data=>{
       wx.hideToast()
        if(data.meat.code==0){
          that.setData({
            address:data.data
          })
        }else{
          wx.showModal({
          title: '温馨提示',
          content: '您还未有收货地址,是否添加?',
          success: function(res) {
            if (res.confirm) {
              wx.navigateTo({
                url: '../user/add/index?oder_user=1',
              })
            }
          }
        })
        }
    })
  },

  //货到付款操作
  evaSubmit:function(e){
    if(this.data.address.length ==0){
      wx.showToast({
        title: '请添加选择收货地址',
        image:'../../assets/images/error-icon.png'
      })
      return
    }
    if(this.data.index == 99999999 && this.data.index_xc== 99999999) {
       wx.showToast({
        title: '请选择相框或者相册',
        image:'../../assets/images/error-icon.png'
      })
      return
    }
    var mark=e.detail.value.mark
    var xk_key=this.data.index
   if(xk_key != 99999999){
        var xk_id=this.data.xk_list[xk_key].id
        if(xk_id == '0'){
          xk_id=''
        }
    }else{
      var xk_id=''
    }
    var xc_key=this.data.index_xc
    if(xc_key != 99999999){
        var xc_id=this.data.xc_list[xc_key].id
        if(xc_id == '0'){
          xc_id=''
        }
    }else{
      var xc_id=''
    }
    var price=this.data.price
    var user_id=wx.getStorageSync('user_id')
    wx.showToast({
      title: '提交中',
      icon: 'loading',
      duration: 10000
    })
    if(xk_id=='' && xc_id ==''){
      wx.showToast({
        title: '请选择相框或者相册',
        image:'../../assets/images/error-icon.png'
      })
      return
    }
    App.HttpService.addOrderInfo({
      user_id:user_id,
      price:price,
      xk_id:xk_id,
      xc_id:xc_id,
      mark:mark,
      pay_type:1 //付款方式
    }).then(data=>{
          if(data.meat.code == 0){
            setTimeout(function(){ 
              //跳转页面
              wx.switchTab({
                url: '/pages/user/index',
              })
            },2000)
          }
    })
  },
  //formReset 微信支付操作
  formReset:function(e){
      if(this.data.address.length ==0){
      wx.showToast({
        title: '请添加选择收货地址',
        image:'../../assets/images/error-icon.png'
      })
      return
    }
    if(this.data.index == 99999999 && this.data.index_xc== 99999999) {
       wx.showToast({
        title: '请选择相框或者相册',
        image:'../../assets/images/error-icon.png'
      })
      return
    }
    var mark=this.data.mark
    var xk_key=this.data.index
    if(xk_key != 99999999){
        var xk_id=this.data.xk_list[xk_key].id
    }else{
      var xk_id=''
    }
    var xc_key=this.data.index_xc
    if(xc_key != 99999999){
        var xc_id=this.data.xc_list[xc_key].id
    }else{
      var xc_id=''
    }
    var price=this.data.price
    var user_id=wx.getStorageSync('user_id')
  
    wx.showToast({
      title: '订单提交中...',
      icon: 'loading',
      duration: 10000
    })
    App.HttpService.addOrderInfo({
      user_id:user_id,
      price:price,
      xk_id:xk_id,
      xc_id:xc_id,
      mark:mark,
      pay_type:2 //付款方式
    }).then(data=>{
          if(data.meat.code == 0){  //订单创建成功下一步   支付
            wx.hideToast()
            wx.navigateTo({
              url: '../order/status?type=1&please=pay&id='+data.data,
            })
          }
    })
  },
  getValue:function(e){
    this.setData({
      mark:e.detail.value
    })
  },
  getPleseaeOrders:function(){
    var that=this
    wx.showToast({
        title: '加载中',
        icon: 'loading',
        duration: 20000,
        mask:true
      })
    App.HttpService.getPleaseOrder({
      user_id:wx.getStorageSync('user_id')
    }).then(data=>{
        wx.hideToast()
        if(data.meat.code == 0){
            if(!data.data){
              wx.showToast({
                title: '暂无数据',
                icon: 'success',
                duration: 2000,
                mask:true
              })
              wx.switchTab({
                url: '/pages/photo/index',
              })
            }
        }
    })
  }
})